﻿using System;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Resn.WhuPub.BIL;
using System.Collections.Generic;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现书籍管理器的属性和方法
        /// </summary>
        public class BookManager:IBookManager
        {
                #region 属性

                private static BookManager _instance;

                /// <summary>
                /// 获取学校/公司管理器实例
                /// </summary>
                /// <value>
                /// 学校/公司管理器实例
                /// </value>
                public static BookManager Instance
                {
                        get { return  _instance??(_instance = new BookManager()); }
                }


                /// <summary>
                /// 获取书籍集合
                /// </summary>
                /// <value>
                /// 书籍集合
                /// </value>
                public List<IBook> Books
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<BookModel>();

                                var categorys = new List<IBook>();

                                models.ForEach(i => categorys.Add(new Book(i)));

                                return categorys;
                        }
                }


                /// <summary>
                /// 获取本社书籍集合
                /// </summary>
                /// <value>
                /// 本社书籍集合
                /// </value>
                public List<IBook> OwnBooks
                {
                        get
                        {
                                return (from i in Books
                                        where i.Publisher == "武汉大学出版社"
                                        select i).ToList();
                        }
                }
                #endregion

                #region 方法



                /// <summary>
                /// 创建书籍
                /// </summary>
                /// <param name="name">名称</param>
                /// <param name="code">代码</param>
                /// <param name="categoryLevel">层次</param>
                /// <returns>
                /// 创建的书籍对象
                /// </returns>
                public IBook CreateBook()
                {
                        var model = new BookModel
                        {
                                Id = Guid.NewGuid().ToString(),
                        };

                        return new Book(model);
                }




                /// <summary>
                /// 从EXCEL文件加载书籍集合
                /// </summary>
                /// <param name="path">EXCEL文件路径</param>
                /// <returns>书籍</returns>
                public Dictionary<IContact, List<IBook>> LoadBooksFromExcel(string path)
                {
                        string[] r = Enum.GetNames(typeof(CategoryLevel));

                        if (!File.Exists(path))
                        {
                                throw new InvalidOperationException("文件不存在，无法加载");
                        }

                        var result = new Dictionary<IContact, List<IBook>>();

                        using (var fileStream = new FileStream(path, FileMode.Open))
                        {
                                var workBook = new HSSFWorkbook(fileStream);
                                var sheet = workBook.GetSheetAt(0);

                                var rowsCount = sheet.PhysicalNumberOfRows;
                                var columnsCount = sheet.GetRow(0).PhysicalNumberOfCells;

                                if (columnsCount < 15)
                                {
                                        throw new InvalidOperationException("列错误，无法导入");
                                }

                                if (rowsCount < 2)
                                {
                                        throw new InvalidOperationException("行错误，无法导入");
                                }

                                for (int i = 1; i < rowsCount; i++)
                                {
                                        IRow row = sheet.GetRow(i);

                                        string bookName = row.Cells[0].StringCellValue;
                                        string seriesName = row.Cells[1].StringCellValue;
                                        string versionType = row.Cells[2].StringCellValue;

                                        string versionTimes = row.Cells[3].StringCellValue;
                                        string printTimes = row.Cells[4].StringCellValue;
                                        string format = row.Cells[5].StringCellValue;

                                        string priceString = row.Cells[6].StringCellValue;
                                        string contactName = row.Cells[7].StringCellValue;
                                        string isbn = row.Cells[8].StringCellValue;

                                        string chargeEditor = row.Cells[9].StringCellValue;
                                        string editorialOffice = row.Cells[10].StringCellValue;
                                        string planningEditor = row.Cells[11].StringCellValue;

                                        string classify = row.Cells[12].StringCellValue;
                                        string createTime = row.Cells[13].StringCellValue;
                                        string contentAbstract = row.Cells[14].StringCellValue;

                                        double price;
                                        bool success = Double.TryParse(priceString, out price);
                                        if (!success)
                                        {
                                                throw new InvalidOperationException(i + "行定价错误，无法导入");
                                        }

                                        throw new NotImplementedException("未完成");

                                        //IContact contact;
                                        //if (result.Keys.Any(j => j.Name == contactName))
                                        //{
                                        //        contact = result.Keys.Single(k => k.Name == contactName);
                                        //}
                                        //else
                                        //{
                                        //        contact = ContactManager.Instance.CreateContact(string.Empty,
                                        //                                                        contactName);
                                        //        result.Add(contact, new List<IBook>());
                                        //}

                                        //IBook book;
                                        //if (result[contact].Keys.Any(j => j.Name == bookName))
                                        //{
                                        //        book = result[contact].Keys.Single(k => k.Name == bookName);
                                        //}
                                        //else
                                        //{
                                        //        book = contact.CreateBook();
                                        //        result[contact].Add(book);
                                        //}
                                }
                        }

                        return result;
                }
                #endregion
        }
}